package com.catering.controller;

import com.catering.config.Result;
import com.catering.pojo.DOrder;
import com.catering.service.DOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private DOrderService dOrderService;

    @PostMapping("/insertOrder")
    @ResponseBody
    public Result insertOrder(@RequestBody DOrder order){
        try {
            // 插入订单并获取ID
            Integer orderId = dOrderService.insertOrder(order);
            return Result.success(orderId);  // 返回新创建的订单ID
        } catch (Exception e) {
            return Result.error("预约失败：" + e.getMessage());
        }
    }

    // 查看我这个订单，根据用户uid和订单id查询，这两个条件
    @GetMapping("/selectByOrder")
    public Result selectByOrder(
            @RequestParam(required = false) Integer uid,
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String orderDate,
            @RequestParam(required = false) String phone
    ) {
        try {
            DOrder orders;
            if (id != null) {
                // 如果有ID，优先使用ID查询
                orders = dOrderService.selectByOrder(uid, id);
            } else {
                // 否则使用时间和电话号码查询
                orders = dOrderService.selectByDateAndPhone(orderDate, phone);
            }
            return Result.success(orders);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    // 取消预约删除我的这条订单，根据订单的id删除
    @DeleteMapping("/deleteByOrder")  // 改为DeleteMapping更规范
    public Result deleteByOrder(@RequestParam("id") Integer id){
        try {
            dOrderService.deleteByOrder(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败：" + e.getMessage());
        }
    }
    @GetMapping("/list")
    public Result list(@RequestParam("uid")Integer uid){
        try {
            // 根据用户uid查询订单列表
            List<DOrder> orders = dOrderService.selectByUid(uid);
            return Result.success(orders);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }
    @GetMapping("selectByOrder1")
    public DOrder selectByOrder(@RequestParam("uid") Integer uid,@RequestParam("id") Integer id){
        DOrder dOrders = dOrderService.selectByOrder1(uid, id);
        return dOrders;
    }
    @GetMapping("/selectByTimeRange")
    public Result selectByTimeRange(
            @RequestParam Integer uid,
            @RequestParam String phone,
            @RequestParam String startTime,
            @RequestParam String endTime
    ) {
        try {
            DOrder order = dOrderService.selectByTimeRange(uid, phone, startTime, endTime);
            return Result.success(order);
        } catch (Exception e) {
            return Result.error("查询失败：" + e.getMessage());
        }
    }
}
